Defining functions and packages

# 1. Source functions ----

source("functions/graphical_par.R")
source("functions/theme_graph.R")
source("functions/pred_vbgf.R")

# 2. Required packages ----

library(tidyverse)
library(kableExtra)
library(formattable)
library(rfishbase)
library(readxl)
library(plotly)

# 3. Set theme_graph() as the default ggplot theme ----

theme_set(theme_graph())

# 4. Load data ----

data_complete <- read.csv("./../data/back-calculated-size-at-age_morat-et-al.csv")

1. Data exploration

1.1 Tables

1.1.1 Data with Back-calc.

data_complete %>% 
  filter(!is.na(Li_sp_m)) %>% 
  summarize_at(vars("ID", "Species", "Family"), n_distinct, na.rm = TRUE) %>% 
  bind_rows(data_complete %>%
              summarize_at(vars("ID", "Species", "Family"), n_distinct, na.rm = TRUE), .) %>% 
  mutate(Type = c("All data", "Back-calculated"), .before = 1) %>% 
  kable(., 
        col.names = c("", "Individual", "Species", "Family"), 
        caption = "Table 1. Comparison of numbers of individuals, species and family for the overall dataset and for back-calculated data") %>% 
  kable_styling(bootstrap_options = c("striped", "hover"))
Table 1. Comparison of numbers of individuals, species and family for the overall dataset and for back-calculated data
Individual Species Family
All data 848 51 16
Back-calculated 709 45 16

1.1.2 Individual by species

data_complete %>%
  group_by(Species) %>% 
  summarise(n = length(unique(ID)),
        min_lencap = round(min(Lcpt, na.rm = TRUE), 0),
        max_lencap = round(max(Lcpt, na.rm = TRUE), 0),
        max_age = max(Agei, na.rm = TRUE)) %>% 
  kable(., 
        col.names = c("Species", "n", "Min length (TL, mm)", "Max length (TL, mm)", "Age max (years)"),
        caption = "Table 2. Number of individuals, minimum and maximum total length (mm) and maximum age, by species") %>%
  kable_styling(bootstrap_options = c("striped", "hover")) %>%
  column_spec(1, italic = T)
Table 2. Number of individuals, minimum and maximum total length (mm) and maximum age, by species
Species n Min length (TL, mm) Max length (TL, mm) Age max (years)
Abudefduf sexfasciatus 16 51 172 13
Acanthurus achilles 10 189 246 27
Acanthurus lineatus 8 123 362 23
Acanthurus nigricans 8 150 210 9
Acanthurus pyroferus 18 51 230 19
Acanthurus triostegus 18 147 194 12
Balistapus undulatus 22 41 283 18
Caranx melampygus 7 368 712 15
Centropyge bispinosa 9 28 80 11
Centropyge flavissima 28 47 150 27
Cephalopholis argus 41 120 450 21
Cephalopholis urodeta 8 150 214 17
Chaetodon citrinellus 13 55 101 6
Chaetodon ornatissimus 10 122 175 10
Cheilinus chlorourus 8 96 299 6
Chlorurus spilurus 34 164 344 16
Chromis iomelas 20 39 61 9
Chromis viridis 6 115 148 9
Ctenochaetus marginatus 15 99 270 13
Ctenochaetus striatus 26 153 218 17
Dascyllus aruanus 20 51 71 7
Dascyllus flavicaudus 8 89 104 13
Epibulus insidiator 17 157 350 16
Epinephelus fasciatus 10 123 256 13
Epinephelus hexagonatus 16 139 268 14
Epinephelus merra 46 108 261 17
Epinephelus polyphekadion 14 252 514 20
Gnathodentex aureolineatus 8 197 255 17
Gymnosarda unicolor 4 499 985 7
Halichoeres trimaculatus 13 64 186 5
Lutjanus fulvus 12 195 289 21
Lutjanus gibbus 17 113 155 1
Lutjanus kasmira 37 70 326 30
Monotaxis grandoculis 9 200 505 20
Mulloidichthys flavolineatus 14 257 311 6
Myripristis berndti 30 147 279 24
Naso lituratus 17 228 432 10
Naso unicornis 4 488 613 17
Odonus niger 18 176 350 16
Ostorhinchus angustatus 15 46 83 6
Ostorhinchus apogonoides 30 52 96 7
Parupeneus barberinus 16 82 496 6
Plectropomus laevis 31 356 957 22
Pristiapogon taeniopterus 32 51 104 8
Sargocentron microstoma 23 62 204 13
Scarus psittacus 8 195 311 6
Siganus argenteus 11 236 388 13
Siganus spinus 4 199 254 3
Stegastes albifasciatus 7 43 94 8
Stegastes nigricans 14 45 138 13
Zebrasoma scopas 18 110 166 24

1.1.3 Individuals by location

data_complete %>%
  group_by(Species, Location) %>% 
  summarise(n = length(unique(ID))) %>% 
  pivot_wider(names_from = Location, values_from = n) %>% 
  mutate(Gambiers = cell_spec(Gambiers, "html", color = ifelse(is.na(Gambiers), "white", "#446CB3")),
         Hao = cell_spec(Hao, "html", color = ifelse(is.na(Hao), "white", "#446CB3")),
         Marquesas = cell_spec(Marquesas, "html", color = ifelse(is.na(Marquesas), "white", "#446CB3")),
         Moorea = cell_spec(Moorea, "html", color = ifelse(is.na(Moorea), "white", "#446CB3")),
         Manuae = cell_spec(Manuae, "html", color = ifelse(is.na(Manuae), "white", "#446CB3")),
         Tuamotu = cell_spec(Tuamotu, "html", color = ifelse(is.na(Tuamotu), "white", "#446CB3"))) %>% 
  kable(., 
        format = "html", escape = FALSE,
        caption = "Table 3. Number of individuals of each species by location") %>%
  kable_styling(bootstrap_options = c("striped", "hover")) %>%
  column_spec(1, italic = T)
Table 3. Number of individuals of each species by location
Species Gambiers Moorea Manuae Marquesas Tuamotu Hao
Abudefduf sexfasciatus 11 5 NA NA NA NA
Acanthurus achilles NA NA 10 NA NA NA
Acanthurus lineatus NA NA NA 8 NA NA
Acanthurus nigricans NA NA NA 8 NA NA
Acanthurus pyroferus NA NA NA 18 NA NA
Acanthurus triostegus 3 11 NA 4 NA NA
Balistapus undulatus NA 14 NA 8 NA NA
Caranx melampygus NA 5 NA NA 2 NA
Centropyge bispinosa NA 9 NA NA NA NA
Centropyge flavissima 9 7 NA 12 NA NA
Cephalopholis argus 6 10 NA NA 10 15
Cephalopholis urodeta 8 NA NA NA NA NA
Chaetodon citrinellus NA 13 NA NA NA NA
Chaetodon ornatissimus 4 NA NA 6 NA NA
Cheilinus chlorourus 2 6 NA NA NA NA
Chlorurus spilurus 13 19 NA NA 2 NA
Chromis iomelas NA 20 NA NA NA NA
Chromis viridis 6 NA NA NA NA NA
Ctenochaetus marginatus NA NA NA 15 NA NA
Ctenochaetus striatus 11 15 NA NA NA NA
Dascyllus aruanus NA 20 NA NA NA NA
Dascyllus flavicaudus 8 NA NA NA NA NA
Epibulus insidiator 12 5 NA NA NA NA
Epinephelus fasciatus NA NA NA 10 NA NA
Epinephelus hexagonatus NA NA NA NA NA 16
Epinephelus merra 13 20 NA NA NA 13
Epinephelus polyphekadion NA NA NA NA NA 14
Gnathodentex aureolineatus 8 NA NA NA NA NA
Gymnosarda unicolor 4 NA NA NA NA NA
Halichoeres trimaculatus 7 6 NA NA NA NA
Lutjanus fulvus NA 6 NA NA 6 NA
Lutjanus gibbus NA NA NA 17 NA NA
Lutjanus kasmira 2 NA NA 35 NA NA
Monotaxis grandoculis 5 3 NA NA 1 NA
Mulloidichthys flavolineatus NA 14 NA NA NA NA
Myripristis berndti 7 15 NA 8 NA NA
Naso lituratus 11 6 NA NA NA NA
Naso unicornis 4 NA NA NA NA NA
Odonus niger NA 13 NA 5 NA NA
Ostorhinchus angustatus NA 15 NA NA NA NA
Ostorhinchus apogonoides NA NA NA 30 NA NA
Parupeneus barberinus 5 4 NA 7 NA NA
Plectropomus laevis 12 NA NA NA NA 19
Pristiapogon taeniopterus NA NA NA 32 NA NA
Sargocentron microstoma NA 17 NA 6 NA NA
Scarus psittacus NA 3 NA NA 5 NA
Siganus argenteus 6 2 NA NA 3 NA
Siganus spinus NA 4 NA NA NA NA
Stegastes albifasciatus NA 7 NA NA NA NA
Stegastes nigricans 3 11 NA NA NA NA
Zebrasoma scopas 5 13 NA NA NA NA

1.1.4 Number of NA

# 1. Number of NA by variable

data_complete %>%
  summarise_all(~(sum(is.na(.)))) %>% 
  t(.) %>% 
  as.data.frame() %>% 
  kable(., col.names = c("NA"), caption = "Table 4. Number of rows with Non-Available (NA) data by variable") %>% 
  kable_styling(bootstrap_options = c("striped", "hover"))
Table 4. Number of rows with Non-Available (NA) data by variable
NA
Family 0
Genus 0
Species 0
ID 0
Agei 0
Ri 377
Agecpt 0
Rcpt 0
Lcpt 0
L0p 0
R0p 2780
Li_sp_m 398
Li_sp_sd 398
Li_sploc_m 745
Li_sploc_sd 745
Weight 603
Location 0
Observer 0

1.2 Plot

1.2.1 Agei vs Ri

ggplot(data_complete, aes(x = Agei, y = Ri)) +
  geom_point(color = col_color_graph, fill = col_fill_graph, size = 1, shape = 21)+
  theme(strip.text.x = element_text(size = 8, face = "italic"),
        strip.background = element_rect(colour = "black", fill = col_facet, size = 1),
        plot.title = element_text(colour = col_color_graph),
        plot.subtitle = element_text(colour = "black")) +
  facet_wrap(~Species, scales = "free", ncol = 5) +
  labs(x = "Age (years)", y = "Radius at age i (mm)", title = "Check for outliers", subtitle = "agei vs radi")

1.2.2 Agei vs Li (species)

ggplot(data_complete, aes(x = Agei, y = Li_sp_m)) +
  geom_point(color = col_color_graph, fill = col_fill_graph, size = 1, shape = 21)+
  theme(strip.text.x = element_text(size = 8),
        strip.background = element_rect(colour = "black", fill = col_facet, size = 1),
        plot.title = element_text(colour = col_color_graph),
        plot.subtitle = element_text(colour = "black"),
        strip.text.y = element_text(angle = 360, face = "italic")) +
  facet_grid(Species~Location, scales = "free") +
  labs(x = "Age (years)", y = "Length at age i (mm)", title = "Check for outliers", subtitle = "agei vs li")

1.2.3 Agei vs Li (sp. and loc.)

ggplot(data_complete, aes(x = Agei, y = Li_sploc_m)) +
  geom_point(color = col_color_graph, fill = col_fill_graph, size = 1, shape = 21)+
  theme(strip.text.x = element_text(size = 8),
        strip.background = element_rect(colour = "black", fill = col_facet, size = 1),
        plot.title = element_text(colour = col_color_graph),
        plot.subtitle = element_text(colour = "black"),
        strip.text.y = element_text(angle = 360, face = "italic")) +
  facet_grid(Species~Location, scales = "free") +
  labs(x = "Age (years)", y = "Length at age i (mm)", title = "Check for outliers", subtitle = "agei vs li")

2. Usual vs Back-calculation

3. Comparison with literature

3.1 Growth curves

# 1. Load and transform literature data ----

vb_litterature <- read_excel("./../data/von-bertalanffy-literature.xlsx", sheet = 1) %>% 
  mutate(Linf = ifelse(Size_unit == "cm", Linf*10, Linf),
         Size_max = ifelse(Size_unit == "cm", Size_max*10, Size_max)) %>% # Convert values of 'Linf' and 'Size_unit' in mm
  select(-Size_unit) %>% 
  mutate(Line = row_number()) %>% 
  group_by(Line) %>% 
  do(pred_vbgf(data = .))

# 2. Make the plot ----

A <- ggplot(data = vb_litterature, aes(x = Agei, y = Li, color = Reference, group = Line)) +
  geom_line(show.legend = FALSE) +
  labs(x = "Age (year)", y = "Length (mm)") +
  facet_wrap(~Genus, scales = "free", ncol = 3) +
  theme(legend.position='none')

# 3. Convert to plotly ----

ggplotly(A)

3.2 Age maximum

Reproducibility

# 1. Reproducibility ----

sessionInfo()
## R version 4.0.1 (2020-06-06)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18363)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=French_France.1252  LC_CTYPE=French_France.1252   
## [3] LC_MONETARY=French_France.1252 LC_NUMERIC=C                  
## [5] LC_TIME=French_France.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] plotly_4.9.2.1      readxl_1.3.1        rfishbase_3.0.4    
##  [4] formattable_0.2.0.1 kableExtra_1.1.0    forcats_0.5.0      
##  [7] stringr_1.4.0       dplyr_1.0.0         purrr_0.3.4        
## [10] readr_1.3.1         tidyr_1.1.0         tibble_3.0.1       
## [13] ggplot2_3.3.2       tidyverse_1.3.0     RColorBrewer_1.1-2 
## [16] extrafont_0.17     
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.4.6      lubridate_1.7.9   lattice_0.20-41   assertthat_0.2.1 
##  [5] digest_0.6.25     R6_2.4.1          cellranger_1.1.0  backports_1.1.7  
##  [9] reprex_0.3.0      evaluate_0.14     highr_0.8         httr_1.4.1       
## [13] pillar_1.4.4      rlang_0.4.6       lazyeval_0.2.2    data.table_1.12.8
## [17] rstudioapi_0.11   extrafontdb_1.0   blob_1.2.1        rmarkdown_2.3    
## [21] labeling_0.3      webshot_0.5.2     htmlwidgets_1.5.1 munsell_0.5.0    
## [25] broom_0.5.6       compiler_4.0.1    modelr_0.1.8      xfun_0.14        
## [29] pkgconfig_2.0.3   htmltools_0.5.0   tidyselect_1.1.0  fansi_0.4.1      
## [33] viridisLite_0.3.0 crayon_1.3.4      dbplyr_1.4.4      withr_2.2.0      
## [37] grid_4.0.1        nlme_3.1-148      jsonlite_1.6.1    Rttf2pt1_1.3.8   
## [41] gtable_0.3.0      lifecycle_0.2.0   DBI_1.1.0         magrittr_1.5     
## [45] scales_1.1.1      cli_2.0.2         stringi_1.4.6     farver_2.0.3     
## [49] fs_1.4.1          xml2_1.3.2        ellipsis_0.3.1    generics_0.0.2   
## [53] vctrs_0.3.1       gh_1.1.0          tools_4.0.1       glue_1.4.1       
## [57] crosstalk_1.1.0.1 hms_0.5.3         yaml_2.2.1        colorspace_1.4-1 
## [61] rvest_0.3.5       memoise_1.1.0     knitr_1.28        haven_2.3.1

Jeremy WICQUART | | 2020-06-25 15:38:50